package cluster.traversal;

import text.Text;
import text.TextSetManager;
import text.traversal.ITextTraversal;

public class ComputeMean implements ITextTraversal
{
	private final static String MEAN = "Mean";
	
	private final TextSetManager textSetManager;
	
	private final String infoName;
	
	private boolean reset;
	
	public void setReset(boolean reset)
	{
		this.reset = reset;
	}
		
	public static String getInfoName(String textName, String infoName)
	{
		return textName + "_" + infoName + "_" + MEAN;
	}
	
	public ComputeMean(String infoName, TextSetManager textSetManager)
	{
		this.infoName = infoName;
		this.textSetManager = textSetManager;
	}
	
	public void traversal(String word, Text text)
	{		
		if(reset)
		{
			for(Text text2 : textSetManager.getTextList())
			{
				String text2Name = getInfoName(text2.getName(), infoName);
				text.addInfo(text2Name, 0d);
			}
			return;
		}
		
		double mean = 0;
		for(Text text2 : textSetManager.getTextList())
		{
			Double value = text2.getInfo(infoName, word);
			if(value == null)
			{
				continue;
			}
			
			String text2Name = getInfoName(text2.getName(), infoName);
			text.addInfo(text2Name, text.getInfo(text2Name) + value);
		}
		
		mean /= textSetManager.getTextCount();
		
		
	}
}
